﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DTO;
using BUS;

public partial class GioHang : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        string text;
        try
        {
            text = Session["UserOnline"].ToString();
        }
        catch (Exception ex)
        {
            Label1.Visible = true;
            Label1.Text = "Khách hàng chưa đăng nhập! Vui lòng đăng nhập để đặt hàng";
            return;
        }

        if (text != "")
        {
            Label1.Visible = false;
            Label1.Text = "";
            DTO.GioHangDTO GioHang = (DTO.GioHangDTO)Session["GioHang"];
            DTO.ThanhVienDTO user = ThanhVienBUS.LayThanhVienTheoUserName(text);

            if (GioHang == null)
                GioHang = new DTO.GioHangDTO();

            if (user == null)
            {
                GioHang.IDNguoiDung = -1;
            }
            else
            {
                GioHang.IDNguoiDung = user.ID;
            }
            String itemID = Request["itemID"];
            if (itemID == null)
            {
                if (GioHang.lstHang.Count == 0)
                    Session["GioHang"] = null;
                return;
            }

            // Xu ly them hang
            DTO.HangDTO hangTemp = new DTO.HangDTO();
            int temp = Int16.Parse(itemID);

            //Tim xem san pham co trong gio hang chua, neu co roi` thi tang so luong
            for (int i = 0; i < GioHang.lstHang.Count; i++)
            {
                if (GioHang.lstHang[i].ID == temp)
                {
                    DTO.SanPhamDTO sanphamTemp = BUS.SanPhamBUS.LaySanPhamTheoID(GioHang.lstHang[i].ID);
                    GioHang.lstHang[i].SoLuong++;
                    Session["GioHang"] = GioHang;
                    return;
                }
            }

            DTO.SanPhamDTO SanPham = BUS.SanPhamBUS.LaySanPhamTheoID(temp);
            GioHang.Ngay = DateTime.Now;
            hangTemp.ID = SanPham.ID;
            hangTemp.SoLuong = 1;
            hangTemp.HinhSanPham = String.Format("Image/{0}", SanPham.HinhSanPham);
            hangTemp.TenSanPham = SanPham.TenSanPham;
            hangTemp.GiaBanHienTai = SanPham.GiaBanHienTai;
            hangTemp.MaDanhMucSanPham = SanPham.MaDanhMucSanPham;
            hangTemp.ID = SanPham.ID;
            hangTemp.MaHangSanXuat = SanPham.MaHangSanXuat;
            hangTemp.SoLuotMua = SanPham.SoLuotMua;
            hangTemp.SoLuotXem = SanPham.SoLuotXem;
            GioHang.lstHang.Add(hangTemp);
            GioHang.ThanhTienTenSanPham.Add(hangTemp.GiaBanHienTai);
            int gianhang = int.Parse(Request["shopID"]);
            
            if (GioHang.Gianhangvaomua.Count == 0)
            {
                GioHang.Gianhangvaomua.Add(gianhang);
            }
            //Tim xem san pham co trong gio hang chua, neu co roi` thi khong them ma gian hang vao danh sach gian hang da mua
            else if (GioHang.Gianhangvaomua.Count > 0)
            {
                for (int i = 0; i < GioHang.lstHang.Count; i++)
                {
                    if (GioHang.lstHang[i].ID == temp && GioHang.Gianhangvaomua[i] == gianhang)
                    {
                        return;
                    }
                    else
                        GioHang.Gianhangvaomua.Add(gianhang);
                }
            }
            Session["GioHang"] = GioHang;
        }
    }

}
